﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Ocmf.Types
{
    public class Folder : Action
    {
        public Folder()
        {
            SelectedImageKey = ImageKey = "Folder";
        }
        public override System.Windows.Forms.Form Editor
        {
            get { throw new NotImplementedException(); }
        }

        public override System.Xml.XmlElement ToXml(System.Xml.XmlDocument xmlDocument)
        {
            //create trial xml element
            var folderElement = xmlDocument.CreateElement("Folder");
            //create, set and append text attribute
            var folderTextAttribute = xmlDocument.CreateAttribute("Name");
            folderTextAttribute.InnerText = Text;
            folderElement.Attributes.Append(folderTextAttribute);
            //iterate over each action
            foreach (Action action in Nodes)
                folderElement.AppendChild(action.ToXml(xmlDocument));
            //return element
            return folderElement;
        }

        public override void FromXml(System.Xml.XmlNode xmlElement)
        {
            Text = xmlElement.Attributes["Name"].Value;
            foreach (XmlNode xmlNode in xmlElement.ChildNodes)
            {
                Action action = null;
                switch (xmlNode.Name)
                {
                    case "Card":
                        action = new Card();
                        break;
                    case "Buzzer":
                        action = new Buzzer();
                        break;
                    case "Food":
                        action = new Food();
                        break;
                    case "GoTo":
                        action = new GoTo();
                        break;
                    case "HouseLight":
                        action = new HouseLight();
                        break;
                    case "Prl":
                        action = new Prl();
                        break;
                    case "Stop":
                        action = new Stop();
                        break;
                    case "Delay":
                        action = new Delay();
                        break;
                    case "Counter":
                        action = new Counter();
                        break;
                }

                action.FromXml(xmlNode);
                Nodes.Add(action);
            }
        }
    }
}
